package it.polimi.swim.servlet;

import it.polimi.swim.entity.Feedback;
import it.polimi.swim.entity.Skill;
import it.polimi.swim.entity.SystemUser;
import it.polimi.swim.session.FeedbackManagerBeanRemote;
import it.polimi.swim.session.FriendManagerBeanRemote;
import it.polimi.swim.session.SkillManagerBeanRemote;
import it.polimi.swim.session.SystemUserManagerBeanRemote;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class SearchServlet
 */
public class SearchServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private Context ctx;
       

    public SearchServlet() {
        super();
        
    }


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
			doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action;
		try{
			Properties env = new Properties();
			env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
			env.put(Context.PROVIDER_URL,"localhost:1099");
			InitialContext jndiContext = new InitialContext(env);
			
			String skill;
			
			ctx = new InitialContext();
			Object ref = ctx.lookup("SystemUserManagerBean/remote");
			SystemUserManagerBeanRemote systemUserManager = (SystemUserManagerBeanRemote) ref;
			Object ref2 = ctx.lookup("SkillManagerBean/remote");
			SkillManagerBeanRemote skillManager = (SkillManagerBeanRemote) ref2;
			Object ref3 = ctx.lookup("FeedbackManagerBean/remote");
			FeedbackManagerBeanRemote feedManager = (FeedbackManagerBeanRemote) ref3;
			
			HttpSession session = request.getSession();
			action=request.getParameter("action");
			
			// get data from request
			List<SystemUser> users = null;
			List<List<Feedback>> feedList=new ArrayList<List<Feedback>>();
			//from user by name form
			if(action.equals("searchUser")){
				String phrase = request.getParameter("user");
				String delims = " ";
				String[] tokens = phrase.split(delims);
				//there is an error
				if(tokens==null || tokens.length!=2){
					String s = "Error in the data entered";
					System.out.println(s);
					request.setAttribute("message", s);
					request.getRequestDispatcher("/messagePage.jsp").forward(request, response);
				
				}
				//input is ok
				else {
					users = systemUserManager.searchUserByName(tokens[0], tokens[1]);
					request.setAttribute("friendList", users);
					
					for(SystemUser u:users){
						feedList.add(feedManager.getFeedbackByUser(u));
					}
					request.setAttribute("feedList", feedList);
					request.getRequestDispatcher("/showUsers.jsp").forward(request, response);
				}
			}
			// from skill user form
			else if(action.equals("usersBySkill")){
				int idSkill = Integer.parseInt(request.getParameter("skill"));
				Skill s = skillManager.getSkillById(idSkill);
				users = s.getUsers();
				request.setAttribute("friendList", users);		
				for(SystemUser u:users){
					feedList.add(feedManager.getFeedbackByUser(u));
				}
				request.setAttribute("feedList", feedList);
				request.getRequestDispatcher("/showUsers.jsp").forward(request, response);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		

	}

}